package com.sinosoft.hydra.um.services.component.bacup;

import com.sinosoft.hydra.um.helper.LoggerHelper;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2016/6/20.
 */
public class MysqlBakupWorker extends BakupWorker {


    public boolean valid(String dirver) {
        return "com.mysql.jdbc.Driver".equals(dirver);
    }

    private String getDbName() {
        int i = url.lastIndexOf("/") + 1, f = url.indexOf("?");
        return f == -1 ? url.substring(i) : url.substring(i, f);
    }

    private String getDbHost(){
        int i = url.lastIndexOf("//") + 2,f = url.lastIndexOf("/");
        String hostStr = url.substring(i, f);
        int m = hostStr.lastIndexOf(":");
        return m == -1 ? hostStr : hostStr.substring(0, m);
    }


    public void bakUp(String path, BakupFactoryBean.BakupListener bakupListener) {
        Runtime runtime = Runtime.getRuntime();
        //-u后面是用户名，-p是密码-p后面最好不要有空格，-family是数据库的名字

        InputStream inputStream = null;
        BufferedReader br = null;
        InputStreamReader reader = null;
        Process process = null;
        try {
            String cmdName = "mysqldump --default-character-set=utf8" +
                    " -u" + username + " -p" + password +
                    " -h" + getDbHost() + " " + getDbName() + " > " + path;
            process = runtime.exec(new String[]{"/bin/sh", "-c", cmdName});
            inputStream = process.getInputStream();//得到输入流，写成.sql文件
            reader = new InputStreamReader(inputStream);
            br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line + "\r\n");
            }
            bakupListener.bakupSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            bakupListener.bacupFail(e);
        } finally {
            try {
                if (process != null) {
                    process.destroy();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (reader != null) {
                    reader.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }


    public void recoveryUp(String path, BakupFactoryBean.RecoveryListener recoveryListener) {
        Process process = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader br = null;
        try {
            Runtime runtime = Runtime.getRuntime();
            //获取mysql连接信息
            String cmdName = "mysql --default-character-set=utf8" +
                    " -u" + username + " -p" + password +
                    " -h" + getDbHost() + " " + getDbName() + " < " + path;
            process = runtime.exec(new String[]{"/bin/sh", "-c", cmdName});
            inputStream = process.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            br = new BufferedReader(inputStreamReader);
            String str;
            while ((str = br.readLine()) != null) {
                System.out.println(str + "\r\n");
            }
            recoveryListener.recoverySuccess();
        } catch (Exception ex) {
            recoveryListener.recoveryFail(ex);
        } finally {
            try {
                if (process != null) {
                    process.destroy();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
